package com.jeesite.modules.home.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.home.dao.DcSupIndexDao;
import com.jeesite.modules.home.entity.DcSupMsgDto;
import com.jeesite.modules.home.entity.DcSupWarningDto;
import com.jeesite.modules.home.entity.EchartsDto;
import com.jeesite.modules.home.entity.cuibanDto;
import com.jeesite.modules.msg.entity.content.PcMsgContent;
import com.jeesite.modules.msg.utils.MsgPushUtils;
import com.jeesite.modules.sys.utils.OfficeUtil;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.dao.DcSupTaskActionDao;
import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.action.entity.DcSupTaskActionDto;
import com.jeesite.modules.task.action.service.DcSupTaskActionService;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.receive.entity.TaskReceiveDto;
import com.jeesite.modules.yzw.service.LehandYzwPushTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.*;

/**
 * 督办首页Service
 *
 * @author xgq
 * @version 2022-03-15
 */
@Service
@Transactional(readOnly=false)
public class DcSupIndexService extends CrudService<DcSupIndexDao, DcSupMsgDto> {

    @Autowired
    private DcSupIndexDao dcSupIndexDao;
    @Autowired
    private SysOfficeUnitService sysOfficeUnitService;
    @Autowired
    private DcSupTaskActionService dcSupTaskActionService;
    @Autowired
    private DcSupTaskInfoService dcSupTaskInfoService;
    @Autowired
    private DcSupTaskActionDao dcSupTaskActionDao;
    @Autowired
    private LehandYzwPushTask lehandYzwPushTask;

    /**
     * 待办已办
     *
     * @return
     */
    public Page<DcSupMsgDto> queryOne(DcSupMsgDto dcSupMsgDto) {
        //获取当前登录人编码
        String userCode = UserUtils.getUser().getUserCode();
        dcSupMsgDto.setReceiveUserCode(userCode);
        List<DcSupMsgDto> dcSupMsgDtoList = dcSupIndexDao.queryOne(dcSupMsgDto);
        return dcSupMsgDto.getPage().setList(dcSupMsgDtoList);
    }

    public List<EchartsDto> queryTwo(DcSupTaskActionDto dcSupTaskActionDto) {
        dcSupTaskActionDto.setTaskYear("");
        dcSupTaskActionDto.setRemarks3(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
        String createTaskDate = dcSupTaskActionDto.getCreateTaskDate();
        if(StringUtils.isNotBlank(createTaskDate)){
            String[] date = createTaskDate.split(" ~ ");
            String startDate = date[0];
            String endDate = date[1];
            dcSupTaskActionDto.setStartTime(startDate);
            dcSupTaskActionDto.setEndTime(endDate);
        }else{
            Calendar cal = Calendar.getInstance();
            String  startTime  =cal.get(Calendar.YEAR) +"-01-01";
            String endTime  = (cal.get(Calendar.YEAR)  + 1)+"-01-01";
            dcSupTaskActionDto.setStartTime(startTime);
            dcSupTaskActionDto.setEndTime(endTime);
        }

        List<EchartsDto> list = dcSupIndexDao.queryTwo(dcSupTaskActionDto);
        //指定列表排序
        List<EchartsDto> echartsDtoList = new ArrayList<>();
        String[] names = {"序时推进", "滞后时序", "已完成", "未完成", "逾期未反馈"};
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            EchartsDto echartsDto = list.stream().filter(x -> x.getName() != null).filter(x -> x.getName().equals(name)).findFirst().orElse(null);
            EchartsDto echarts = Optional.ofNullable(echartsDto).orElse(new EchartsDto(name, 0));
            echartsDtoList.add(echarts);
        }
        return echartsDtoList;
    }
    /**
     * 查询任务逾期信息
     *
     * @param dcSupWarningDto
     * @return
     */
    public List<DcSupWarningDto> queryThree(DcSupWarningDto dcSupWarningDto) {
        //获取当前登录人code
        String createBy = UserUtils.getUser().getUserCode();
        dcSupWarningDto.setCreateBy(createBy);
        return dcSupIndexDao.queryThree(dcSupWarningDto);
    }

    public Page<DcSupWarningDto> queryThreePage(DcSupWarningDto dcSupWarningDto) {
        //获取当前登录人code
        String createBy = UserUtils.getUser().getUserCode();
        dcSupWarningDto.setCreateBy(createBy);
        List list = dcSupIndexDao.queryThree(dcSupWarningDto);
        return dcSupWarningDto.getPage().setList(list);
    }

    public List<EchartsDto> queryFive(String taskYear,String flag) {
        return dcSupIndexDao.queryFive(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode(), taskYear,flag);
    }
//    static Map querSixMap = new HashMap();
    public List<EchartsDto> querySix(String taskYear,String flag) {
//       String officeCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode()==null?"":sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
//        String officeCodeTaskYearKey=officeCode+"_"+taskYear;
//        if(!querSixMap.containsKey(officeCodeTaskYearKey)){
//            querSixMap.put(officeCodeTaskYearKey,dcSupIndexDao.querySix(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode(), taskYear,flag));
//            //创建Timer
//            final Timer timer = new Timer();
//            //设定定时任务
//            timer.schedule(new TimerTask() {
//                //定时任务执行方法
//                @Override
//                public void run() {
//                    querSixMap.remove(officeCodeTaskYearKey);
//                }
//            }, 1000*60*10);
//        }
//        List<EchartsDto> echart =(List<EchartsDto>)  querSixMap.get(officeCodeTaskYearKey);
        List<EchartsDto> echart= dcSupIndexDao.querySix(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode(), taskYear,flag);
        return echart;
    }

    /**
     * 查询通知公告
     *
     * @param dcSupMsgDto
     * @return
     */
    public Page<DcSupMsgDto> queryFour(DcSupMsgDto dcSupMsgDto) {
        //获取当前登录人编码
        String userCode = UserUtils.getUser().getUserCode();
        dcSupMsgDto.setReceiveUserCode(userCode);
        List<DcSupMsgDto> dcSupMsgDtoList = dcSupIndexDao.queryFour(dcSupMsgDto);
        return dcSupMsgDto.getPage().setList(dcSupMsgDtoList);
    }

    public Page<DcSupWarningDto> queryGatewayWarning(DcSupWarningDto dcSupWarningDto) {
        if("1".equals(dcSupWarningDto.getLimit())){
            dcSupWarningDto.getPage().setPageSize(5);
        }
        String officeCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        dcSupWarningDto.setRemarks2(OfficeUtil.getCurrentOffice().getOfficeCode());
        dcSupWarningDto.setRemarks3(officeCode);
        String year = String.valueOf(LocalDate.now().getYear());
        dcSupWarningDto.setTaskYear(year);
        List<DcSupWarningDto> list = dcSupIndexDao.queryGatewayWarning(dcSupWarningDto);
        return dcSupWarningDto.getPage().setList(list);
    }

    public List<EchartsDto> queryGatewayProgress() {
        String userCode = UserUtils.getUser().getUserCode();
        String officeCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        String year = String.valueOf(LocalDate.now().getYear());
        List<EchartsDto> list = dcSupIndexDao.queryGatewayprogress(year,officeCode, userCode);
        List<EchartsDto> res = new ArrayList<>();
        String[] names = {"序时推进", "滞后时序", "已完成", "未完成", "逾期未反馈"};
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            EchartsDto echartsDto = list.stream().filter(x -> x.getName() != null).filter(x -> x.getName().equals(name)).findFirst().orElse(null);
            EchartsDto echarts = Optional.ofNullable(echartsDto).orElse(new EchartsDto(name, 0));
            res.add(echarts);
        }
        return res;
    }

    public Page<TaskReceiveDto> supIndexFiveList(TaskReceiveDto taskReceiveDto) {
        List<TaskReceiveDto> list = dcSupIndexDao.supIndexFiveList(taskReceiveDto);
        return taskReceiveDto.getPage().setList(list);
    }

    public Page<TaskReceiveDto> supIndexSixList(TaskReceiveDto taskReceiveDto) {
        List<TaskReceiveDto> list = dcSupIndexDao.supIndexSixList(taskReceiveDto);
        return taskReceiveDto.getPage().setList(list);
    }

    /**
     * 督办首页-总体督办事项数量
     *
     * @return
     */
    public List<EchartsDto> querySeven(String taskYear) {
        return dcSupIndexDao.querySeven(taskYear);
    }

    /**
     * 督办首页-单位承担督办事项TOP10排名
     *
     * @return
     */
    public List<EchartsDto> queryNine(String taskYear) {
        return dcSupIndexDao.queryNine(taskYear);
    }

    public Page<DcSupTaskActionDto> queryTwoPage(DcSupTaskActionDto dcSupTaskActionDto) {
        dcSupTaskActionDto.setRemarks3(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
        if(StringUtils.isNotBlank(dcSupTaskActionDto.getCreateTaskDate())){
            String[] date = dcSupTaskActionDto.getCreateTaskDate().split(" ~ ");
            String startDate = date[0];
            String endDate = date[1];
            dcSupTaskActionDto.setStartTime(startDate);
            dcSupTaskActionDto.setEndTime(endDate);
        } else{
            Calendar cal = Calendar.getInstance();
            String  startTime  =cal.get(Calendar.YEAR) +"-01-01";
            String endTime  = (cal.get(Calendar.YEAR)  + 1)+"-01-01";
            dcSupTaskActionDto.setStartTime(startTime);
            dcSupTaskActionDto.setEndTime(endTime);
        }
        if("序时推进".equals(dcSupTaskActionDto.getRemarks2())){
            dcSupTaskActionDto.setRemarks2("1");
        }else if("滞后时序".equals(dcSupTaskActionDto.getRemarks2())){
            dcSupTaskActionDto.setRemarks2("2");
        }else if("已完成".equals(dcSupTaskActionDto.getRemarks2())){
            dcSupTaskActionDto.setRemarks2("3");
        }else if("未完成".equals(dcSupTaskActionDto.getRemarks2())){
            dcSupTaskActionDto.setRemarks2("4");
        }else if("逾期未反馈".equals(dcSupTaskActionDto.getRemarks2())){
            dcSupTaskActionDto.setRemarks2("5");
        }else{
            dcSupTaskActionDto.setRemarks2(null);
            List<DcSupTaskActionDto> list = dcSupIndexDao.queryTwoPageAll(dcSupTaskActionDto);
            return dcSupTaskActionDto.getPage().setList(list);
        }

        List<DcSupTaskActionDto> list = dcSupIndexDao.queryTwoPage(dcSupTaskActionDto);
        return dcSupTaskActionDto.getPage().setList(list);
    }

    public Page<DcSupTaskActionDto> queryGatewayProgressPage(DcSupTaskActionDto dcSupTaskActionDto) {
        String userCode = UserUtils.getUser().getUserCode();
        String officeCode = sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode();
        dcSupTaskActionDto.setRemarks2(userCode);
        dcSupTaskActionDto.setRemarks3(officeCode);
        if("序时推进".equals(dcSupTaskActionDto.getRemarks4())){
            dcSupTaskActionDto.setRemarks4("1");
        }else if("滞后时序".equals(dcSupTaskActionDto.getRemarks4())){
            dcSupTaskActionDto.setRemarks4("2");
        }else if("已完成".equals(dcSupTaskActionDto.getRemarks4())){
            dcSupTaskActionDto.setRemarks4("3");
        }else if("未完成".equals(dcSupTaskActionDto.getRemarks4())){
            dcSupTaskActionDto.setRemarks4("4");
        }else if("逾期未反馈".equals(dcSupTaskActionDto.getRemarks4())){
            dcSupTaskActionDto.setRemarks4("5");
        }
        // 任务进展跳转的数据，加上当前年份
        dcSupTaskActionDto.setTaskYear(DateUtils.getYear());

        List<DcSupTaskActionDto> list = dcSupIndexDao.queryGatewayProgressPage(dcSupTaskActionDto);
        return dcSupTaskActionDto.getPage().setList(list);
    }

    public void cuiban(cuibanDto cuiban) {
        // 获取任务分发信息
        DcSupTaskAction action = dcSupTaskActionService.get(cuiban.getId());
        // 获取任务信息
        DcSupTaskInfo info = dcSupTaskInfoService.get(action.getTaskId());
        // 获取任务转办人
        String transferUserCode = action.getTransferOrgCode();
        // 督办首页-任务逾期信息催办
        String message = cuiban.getMessage();
        for (int i = 0; i < cuiban.getUserList().size(); i++) {
            if(StringUtils.isBlank(cuiban.getUserList().get(i).getPhone()) || StringUtils.isBlank(cuiban.getUserList().get(i).getName())){
                continue;
            }
            cuiban.setPhone(cuiban.getUserList().get(i).getPhone());
            cuiban.setUserCode(cuiban.getUserList().get(i).getUserCode());
            PcMsgContent msg = new PcMsgContent();
            msg.setTitle("提示信息");
            if (StringUtils.equals(transferUserCode, cuiban.getUserList().get(i).getUserCode())) {
                // 如果是转办人，则跳转到任务待办页面
                msg.addButton("任务待办", "/a/action/dcSupTaskAction/noCompleteView");
            } else {
                // 如果不是转办人，跳转到任务接收页面
                msg.addButton("任务接收", "/a/audit/taskReceive/list");
            }
            msg.setContent(message);
            MsgPushUtils.push(msg, info.getId() + "_" + action.getLeaderOrgCode(), "feedback_msg", cuiban.getUserList().get(i).getUserCode());
            //发送易政网消息
           // cuiban.setMessage(cuiban.getUserList().get(i).getName() + ",您有一个任务:\"" + info.getTaskTitle() + ",请及时登录系统查看");
            cuiban.setMessage(cuiban.getUserList().get(i).getName() + ","+cuiban.getMessage());
            lehandYzwPushTask.pushYzwShortMsg2(cuiban);
        }
    }

    /**
     *
     * @param cuiban
     */
    public void cuiban2(cuibanDto cuiban) {
        String message = cuiban.getMessage();
        for (int i = 0; i < cuiban.getUserList().size(); i++) {
            cuiban.setPhone(cuiban.getUserList().get(i).getPhone());
            cuiban.setUserCode(cuiban.getUserList().get(i).getUserCode());
            PcMsgContent msg = new PcMsgContent();
            msg.setTitle("提示信息");
//            msg.addButton("任务接收", "/a/audit/taskReceive/list");
            msg.setContent(message);
            MsgPushUtils.push(msg, cuiban.getId(), "pingfenhuizongcuiban", cuiban.getUserList().get(i).getUserCode());
            //发送易政网消息
            cuiban.setMessage(cuiban.getUserList().get(i).getName() + ","+cuiban.getMessage());
           // cuiban.setMessage(cuiban.getUserList().get(i).getName() + ",您有一个评分被催办,请及时登录系统查看");
            lehandYzwPushTask.pushYzwShortMsg2(cuiban);
        }
    }

    public List<Map> queryOrgRank() {
       return dao.queryOrgRank();
    }
}
